package com.xjh.mange.controller;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xjh.mange.common.enums.DeleteStatusEnum;
import com.xjh.mange.model.Password;
import com.xjh.mange.model.Web;
import com.xjh.mange.model.system.Server;
import com.xjh.mange.service.GoodsService;
import com.xjh.mange.service.PasswordService;
import com.xjh.mange.service.WebService;
import com.xjh.mange.vo.ResponseVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;

/**
 * @author 徐佳辉
 * @date 2020/12/20 23:14
 */
@Controller
@RequestMapping("/count")
@Slf4j
public class CountController {

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private PasswordService passwordService;
    @Autowired
    private WebService webService;


    /**
     * 首页统计
     *
     * @return
     */
    @GetMapping("get")
    @ResponseBody
    public ResponseVO get() {
        String s = HttpUtil.get("http://192.168.1.11:8002/count");
        JSONObject jsonObject = JSONObject.parseObject(s);
        int count = (int) jsonObject.get("count");
        int goods = goodsService.count();
        QueryWrapper<Web> webQueryWrapper = new QueryWrapper<>();
        webQueryWrapper.eq("del", DeleteStatusEnum.NOT_DELETE.getCode());
        int web = webService.count(webQueryWrapper);
        QueryWrapper<Password> passwordQueryWrapper = new QueryWrapper<>();
        passwordQueryWrapper.eq("del", DeleteStatusEnum.NOT_DELETE.getCode());
        int password = passwordService.count(passwordQueryWrapper);
        HashMap hashMap = new HashMap();
        hashMap.put("good", goods);
        hashMap.put("web", web);
        hashMap.put("password", password);
        hashMap.put("article", count);
        return ResponseVO.ok(hashMap);
    }


    /**
     * 系统信息
     *
     * @return
     */
    @GetMapping("/system")
    @ResponseBody
    public ResponseVO systemInfo() throws Exception {
        Server server = new Server();
        server.serverInfo();
        return ResponseVO.ok(server);
    }

    @GetMapping("/m2")
    @ResponseBody
    public ResponseVO systemInfo2() throws Exception {
        Server server = new Server();
        server.serverInfo();
        return ResponseVO.ok(server);
    }

}
